﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core.DS
{
    public static partial class experiment
    {
        public static dynamic getExperimentList(int start, int limit, string username)
        {
            var sql = new Sql.SqlDataContext();
            var total = sql.DSExperiment.Count();
            IQueryable result;
			if (!string.IsNullOrEmpty(username)) //User
                result = (from e in sql.DSExperiment
                          orderby e._order
                          let pnum = sql.DSProblem.Count(row => row.eid == e.eid)
                          let acnum = sql.DSSolve.Count(row => row.eid == e.eid && row.username == username && row.accept > 0)
                          let hasub = sql.DSSolve.Any(row => row.eid == e.eid && row.username == username && row.submit > 0)
                          let report = sql.DSExperimentSolve.Any(row => row.eid == e.eid && row.username == username && row.report)
                          select new
                          {
                              e.eid,
                              e.title,
                              e.description,
                              startTime = e.startTime.toTimeString(),
                              endTime = e.endTime.toTimeString(),
                              status = acnum >= pnum && report ? 3 : acnum >= pnum ? 2 : hasub ? 1 : 0
                          }).Skip(start).Take(limit);
            else //Guest
                result = (from e in sql.DSExperiment
                          orderby e._order
                          select new
                          {
                              e.eid,
                              e.title,
                              e.description,
                              startTime = e.startTime.toTimeString(),
                              endTime = e.endTime.toTimeString(),
                              status = 0
                          }).Skip(start).Take(limit);
            return new
            {
                success = true,
                total,
                result
            };
        }
    }
}
